#pragma once
#include<iostream>
#include<vector>

using namespace std;

namespace cyq
{

	//仿函数/函数对象
	template<class T>
	class Less
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	template<class T>
	class Greater
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};

	template<class T>
	class LessDate
	{
	public:
		bool operator()(const T& p1, const T& p2)
		{
			return *p1 < *p2;
		}
	};

	template<class T>
	class GreaterDate
	{
	public:
		bool operator()(const T& p1, const T& p2)
		{
			return *p1 > *p2;
		}
	};


	template<class T, class container = vector<T>, class compare = Less<T>>
	class priority_queue
	{
	private:
		void AdjustDown(int parent)
		{
			compare com;
			int child = 2 * parent + 1;
			if (child + 1 < _con.size() && com(_con[child],_con[child+1]))
			{
				child++;
			}

			while (child < _con.size())
			{
				if (com(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					parent=child;
					child=2*parent+1;
				}
				else
				{
					break;
				}
			}
		}

		void AdjustUp(int child)
		{
			compare com;

			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if(com(_con[parent],_con[child]))
				{
					swap(_con[parent], _con[child]);
					child=parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}
	public:
		priority_queue()
		{

		}

		//使用迭代区间构造
		template<class InputIterator >
		priority_queue(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				_con.push_back(*first);
				first++;
			}

			//向下调整建堆
			for (size_t i = (_con.size() - 1 - 1) / 2; i >=0; i--)
			{
				AdjustDown(i);
			}
		}

		void push(const T& x = T())
		{
			_con.push_back(x);
			AdjustUp(_con.size()-1);
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			AdjustDown(0);
		}

		size_t size()
		{
			return _con.size();
		}

		T& top()
		{
			return _con.front();
		}

		bool empty()
		{
			return _con.empty();
		}

	private:
		container _con;
	};

}
